﻿using AutoMapper;
using MediatR;
using Traceable.App.Application.Command.Processing;
using Traceable.App.DTOS;
using Traceable.Domian;
using Traceable.ErrorCode;
using Traceable.Infastructure;

namespace Traceable.App.Application.Handler.Processing
{
    public class GetSupplierListHandler : IRequestHandler<GetSupplierListCommand, ApiResult<SupplierDto>>
    {
        private readonly IBaseRepository<Suppliers_Table> _supplierRepo;
        private readonly IMapper _mapper;

        public GetSupplierListHandler(IBaseRepository<Suppliers_Table> supplierRepo, IMapper mapper)
        {
            _supplierRepo = supplierRepo;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<SupplierDto>> Handle(GetSupplierListCommand request, CancellationToken cancellationToken)
        {
            ApiResult<SupplierDto>result = new ApiResult<SupplierDto>();

            var supplier = _supplierRepo.GetAll().Where(p => p.Batch_Id == request.Batch_Id && p.Suppliers_Type == request.Suppliers_Type).FirstOrDefault();
            result.Code = StatusEnum.OK;
            result.Data=_mapper.Map<SupplierDto>(supplier);
            result.Message = "获取成功";
            return Task.FromResult(result);
        }
    }
}
